﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam2.Biz;
using MockPrjTeam2.DAO;

namespace MockPrjTeam2.Pages.Views
{
    public partial class Service : System.Web.UI.Page
    {
        ServiceBiz serviceBiz = new ServiceBiz();
        private static int _flag;
        private static int _check;
        private static string _key = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _flag = 0;
                _check = 0;
                BindingData();
            }
        }

        private void BindingData()
        {
            grvService.DataSource = serviceBiz.GetAll();
            grvService.DataBind();
        }

        protected void LbtnServiceNameClick(object sender, CommandEventArgs e)
        {
            if (e.CommandArgument.Equals("False"))
            {
                var serviceId = int.Parse(e.CommandName, CultureInfo.CurrentCulture);
                bool result = serviceBiz.SetActiveService(serviceId, true);
            }
            Response.Redirect(string.Format(CultureInfo.CurrentCulture, "../Forms/ServiceDetail.aspx?id={0}", e.CommandName));
        }

        protected void StartWithByNames(object sender, CommandEventArgs e)
        {
            if (chkInActive.Checked)
            {
                _check = 1;
                _flag = 1;
                _key = e.CommandName;
                grvService.DataSource = serviceBiz.StartWithByNames(_key, _flag);
                grvService.DataBind();
            }
            else
            {
                _check = 1;
                _flag = 0;
                _key = e.CommandName;
                grvService.DataSource = serviceBiz.StartWithByNames(_key, _flag);
                grvService.DataBind();
            }
            
        }

        protected void btnCreateService_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/Forms/AddService.aspx");
        }

        protected void grvService_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvService.PageIndex = e.NewPageIndex;
            if (_check == 1)
            {
                if (chkInActive.Checked && _check == 1)
                {
                    _flag = 1;
                    grvService.DataSource = serviceBiz.StartWithByNames(_key, _flag);
                }
                else if (chkInActive.Checked == false && _check == 1)
                {
                    _flag = 0;
                    grvService.DataSource = serviceBiz.StartWithByNames(_key, _flag);

                }

            }
            else if (_check == 0)
            {
                if (chkInActive.Checked && _check == 0)
                {
                    grvService.DataSource = serviceBiz.GetByInActive();
                }
                else
                {
                    _flag = 0;
                    grvService.DataSource= serviceBiz.GetAll();
                }
            }
            grvService.DataBind();
        }

        protected void chkInActive_CheckedChanged(object sender, EventArgs e)
        {
            if (_check == 1)
            {
                if (chkInActive.Checked && _check == 1)
                {
                    _flag = 1;
                    grvService.DataSource = serviceBiz.StartWithByNames(_key, _flag);
                    grvService.DataBind();
                }
                else if (chkInActive.Checked == false && _check == 1)
                {
                    _flag = 0;
                    grvService.DataSource = serviceBiz.StartWithByNames(_key, _flag);
                    grvService.DataBind();

                }
              
            }
            else if (_check == 0)
            {
                if (chkInActive.Checked && _check == 0)
                {
                    grvService.DataSource = serviceBiz.GetByInActive();
                    grvService.DataBind();
                }
                else
                {
                    _flag = 0;
                    BindingData();
                }
            }
        }
    }
}